<?php

namespace app\admin\controller;

use app\admin\controller\AdminController;
use think\Request;
use think\Db;

class Link extends AdminController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $data = Db::name('links')->paginate(3);
        return view('link/index',[
            'title' => '友情链接',
            'list' => $data
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return view('link/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $req = $request->post();
        $data = [
            "linksname" => $req['linksname'],
            "linksurl" => $req['linksurl']
        ];
        $status = Db::name('links')->where('linksname','=',$data['linksname'])->select();
        $status2 = Db::name('links')->where('linksurl','=',$data['linksurl'])->select();

        if(!empty($status) || !empty($status2)){
            echo "<script>alert('添加失败,友情链接已经存在');window.history.back();</script>";
        }else{
            $add = Db::name('links')->insert($data);
            if ($add > 0){
                return "<script>alert('添加成功');window.history.back()</script>";
            }else {
                return "<script>alert('添加失败');window.history.back()</script>";
            }
        }

    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit()
    {
        $list = Db::name('links')->find($_POST['id']);

        $data = [
            'links_id'=>$list['links_id'],
            'linksname'=>$list['linksname'],
            'linksurl'=>$list['linksurl']
        ];

        return json($data);

        // return view('link/update',[
        //     'links_id' => $data['links_id'],
        //     'linksname' => $data['linksname'],
        //     'linksurl' => $data['linksurl'],
        //
        // ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update()
    {

        $data = [
            "links_id" => $_POST['id'],
            "linksname" => $_POST['name'],
            "linksurl" => $_POST['url']
        ];

        $update = Db::name('links')->where('links_id', $_POST['id'])->update($data);
        if ($update > 0){
            $info['status'] = true;
            $info['info'] = 'ID为: ' . $_POST['id'] . '的链接更新成功!';
        }else {
            $info['status'] = false;
            $info['info'] = 'ID为: ' . $_POST['id'] . '的链接更新失败!';
        }
        return json($info);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $del = Db::name('links')->delete($id);
        if ($del > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的用户删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的用户删除失败,请重试!';
        }
        return json($info);
    }
}
